using UnityEngine;

public class BaseAnimatedValue : MonoBehaviour
{
	[SerializeField]
	protected float speed = 2f;

	[SerializeField]
	protected float _percent;

	[SerializeField]
	protected bool toOne;

	[SerializeField]
	protected bool autoLoop = true;

	[SerializeField]
	protected float waitInState = 5f;

	protected bool isWriting = true;

	private float akkum;

	public float Percent
	{
		get
		{
			return _percent;
		}
		set
		{
			_percent = Mathf.Clamp(value, 0f, 1f);
		}
	}

	public virtual void ToOne()
	{
		toOne = true;
	}

	public virtual void ToZero()
	{
		toOne = false;
	}

	public virtual void SetPercent()
	{
	}

	public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		isWriting = stream.isWriting;
		if (isWriting)
		{
			stream.SendNext(Percent);
			stream.SendNext(toOne);
			return;
		}
		float num = (float)stream.ReceiveNext();
		bool flag = (bool)stream.ReceiveNext();
		if (Mathf.Abs(Percent - num) > 0.2f)
		{
			Percent = num;
		}
		toOne = flag;
	}

	private void Update()
	{
		SetPercent();
		if (isWriting && autoLoop && (Percent == 0f || Percent == 1f))
		{
			if (akkum > waitInState)
			{
				akkum = 0f;
				if (Percent == 0f)
				{
					ToOne();
				}
				else
				{
					ToZero();
				}
			}
			else
			{
				akkum += Time.deltaTime;
			}
		}
		float num = speed * Time.deltaTime;
		if (!isWriting)
		{
			num += num * (float)PhotonNetwork.GetPing() / 1000f;
		}
		if (toOne)
		{
			Percent += num;
		}
		else
		{
			Percent -= num;
		}
	}
}
